package com.bailiban.aigou.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bailiban.aigou.admin.dao.SearchHistoryDao;
import com.bailiban.aigou.admin.entity.SearchHistory;
import com.bailiban.aigou.admin.service.ISearchHistoryService;
import com.bailiban.aigou.admin.utils.DateUtils;
@Service
public class SearchHistoryServiceImpl implements ISearchHistoryService {
	@Autowired
	private SearchHistoryDao searchHistoryDao;
	
	/**
	 * 获取指定条数的热搜词
	 * @param num
	 * @return
	 * @
	 */
	@Transactional
	public List<SearchHistory> getSearchHistoryPages(int num) {
		
		return searchHistoryDao.getSearchHistoryPages(num);
	}
	/**
	 * 新增热搜词
	 * @param searchHistory
	 * @
	 */
	@Transactional
	public void addSearchHistory(SearchHistory searchHistory) {
		
		searchHistoryDao.addSearchHistory(searchHistory);
	}
	/**
	 * 热搜词是否存在
	 * @param search
	 * @return
	 * @
	 */
	@Transactional
	public boolean isExist(String searchWords) {
		
		return searchHistoryDao.isExist(searchWords);
	}
	/**
	 * 更新热搜词次数（每次+1）
	 * @param search
	 * @
	 */
	@Transactional
	public void updateSearchHistory(String searchWords) {
		searchHistoryDao.updateSearchHistory(searchWords);
		

	}
	/**
	 * 新增或更新
	 * @param search
	 * @
	 */
	@Transactional
	public void addOrUpdateSearchHistory(String searchWords) {
		
		boolean exist = searchHistoryDao.isExist(searchWords);
		if (exist) {
			searchHistoryDao.updateSearchHistory(searchWords);
		}else {
			SearchHistory searchHistory = new SearchHistory();
			
			searchHistory.setNum(1);
			searchHistory.setSearchTime(DateUtils.getDateTimeString());
			searchHistory.setSearchWords(searchWords);
			searchHistoryDao.addSearchHistory(searchHistory);
		}
	}
	

}
